<?php

namespace app\merchants\services;

use app\common\loader\ModelShop;
use think\db\exception\DbException;
use think\Exception;
use yiqiniu\extend\facade\Db;

/**
 * 导购管理
 */
class ShopSalesmanService
{


    /**
     *  分页列表
     * @param     $where |条件
     * @param int $page |分页
     * @param int $page_size |数量
     * @throws Exception
     */
    public function salesmanPage($where, int $page = 1, int $page_size = 30)
    {

        $list = ModelShop::ShopSalesman()->salesmansPage($where,  $page,  $page_size);
        if (!empty($list['data'])) {
            $this->showShopTitle($list['data']);
        }
        return $list;
    }


    /**
     *  分页列表
     * @param     $where |条件
     * @param int $list
     * @return array|mixed
     * @throws Exception
     */
    public function salesmanList($where, int $list = 10)
    {

        return ModelShop::ShopSalesman()->salesmanList($where, $list = 10);
    }

    /**
     *  详情
     * @param $id |主键
     * @throws Exception
     */
    public function salesmanInfo($id)
    {
        return ModelShop::ShopSalesman()->salesmanInfo($id);
    }

    /**
     *修改
     * @param $shop_id |门店
     * @param $mer_id | 商户
     * @param $name |昵称
     * @param $mobile |手机号
     * @param $id |主键
     * @throws Exception
     */
    public function salesmanSave($shop_id, $mer_id, $name, $mobile, $id = 0)
    {

        return ModelShop::ShopSalesman()->salesmanSave($shop_id, $mer_id, $name, $mobile, $id);

    }


    /**
     * 积分调整
     * @param $id |主键
     * @param $type |1添加， 2减少
     * @param $point |积分
     * @throws Exception
     */
    public function salesmanSavePoint($id, $point, $memo, $type)
    {

        try {
            Db::startTrans();
            $result = ModelShop::ShopSalesman()->salesmanSavePoint($id, $point, $memo, $type);
            Db::commit();
            return $result;

        } catch (Exception $e) {
            Db::rollback();
            throw   $e;
        }

    }


    /**
     * 取消绑定
     * @param $id |主键
     * @param $status |1取消， 2绑定
     * @throws DbException
     */
    public function saveBind($id, $status)
    {

        return ModelShop::ShopSalesman()->where('id', $id)
            ->update(null, ['status' => $status]);
    }


    /**
     * 清除绑定信息
     * @param $id |主键
     * @param $mer_id
     * @param $data
     * @return int|string
     * @throws DbException
     */
    public function eliminateWxInfo($id, $mer_id, $data)
    {

        return ModelShop::ShopSalesman()->where(['id' => $id, 'mer_id' => $mer_id])
            ->update(null, $data);
    }

    /**
     * 修改导购信息
     * @param array $params
     * @param string $mer_id
     * @return int|string
     * @throws DbException
     * @throws Exception
     */
    public function editShopper(array $params, string $mer_id)
    {
        $id = $params['id'];
        unset($params['id'], $params['open_id'], $params['lat'], $params['lng']);
        $model = ModelShop::ShopSalesman();
        $data = $model->getSalesmanUserInfo(['mer_id' => $mer_id, 'id' => $id], ['*']);

        if (!empty($params['recheck']) && $params['recheck'] == 1) {
            if (empty($data['open_id'])) {
                api_exception(400, '导购未绑定微信');
            }
            if (!empty($params['mobile'])) {
                $check = $model->checkMobile($mer_id, $params['mobile']);
                if (!empty($check) && $check != $id) {
                    api_exception(400, '手机号已存在');
                }
            }
            if (empty($params['shop_id'])) {
                api_exception(400, '门店不能为空');
            }
            $shop = ModelShop::Shop()->where('id', $params['shop_id'])->where('mer_id', $mer_id)->where('status', 1)->find();
            if (empty($shop)) {
                api_exception(400, '门店不存在或已禁用');
            }
            $params['status'] = 1;
        }

        if ($data['status'] !== $params['recheck']) {
            ModelShop::Shop()->saveSalesNum($params['shop_id'], (int)$params['recheck']);
        }
        if ($params['recheck'] === 2) {
            unset($params['mobile']);
        }
        $result = $model->removeOption()->where(['id' => $id, 'mer_id' => $mer_id])
            ->update(null, $params);

        // 审核成功时,发送通知
        if (!empty($params['status']) && $params['status'] === 1) {
            event('RecheckSuccess', []);
        }
        return $result;
    }

    /**
     * 展示门店信息
     */
    private function showShopTitle(&$list)
    {
        $shop_id = array_column($list, 'shop_id');
        $shop = empty($shop_id) ? [] : ModelShop::Shop()->whereIn('id', $shop_id)->column(null, 'title', 'id');
        foreach ($list as &$v) {
            $v['shop_title'] = $shop[$v['shop_id']] ?? '未分配';
        }
    }
}
